/**
 * Created by Mars on 17/4/12.
 */


(function($, win, doc) {
    function Material() {
        this.type = {
            'video':'video',
            'music':'music',
            'img':'img'
        }
        this.Init();
    };
    Material.prototype.Init = function () {
        var _this = this;
        _this.GetMaterialList(_this.type.video)
            .done(function (data) {
                if(data.code == 0){
                    _this.RenderVideo(data.data);
                }
            });

    }
    Material.prototype.GetMaterialList = function (type) {
        return $.ajax({
            url: config_global.materialList_address,
            type: 'POST',
            dataType: 'jsonp',
            data: {
                type: type.toString()
            }
        })
    };
    Material.prototype.GetMaterial = function(id) {
        return $.ajax({
            url: config_global.materialInfo_address,
            type: 'GET',
            dataType: 'jsonp',
            data: {
                material_id: id
            }
        })
    };
    Material.prototype.RenderVideo = function (data) {
        var _this = this;
        for(var i=0 ;i<data.length;i++){
            //获取每一个视频的详细信息
            _this.GetMaterial(data[i].material_id)
                .done(function (res) {
                    if(res.code == 0){
                        allData_g.push(res.data);
                        videoData_g.push(res.data);

                        $('.videolib-lib-local-list').prepend(_this.GetVideoTemplate(res.data));
                        _this.events(res.data.material_id);
                    }
                });


        }
    }
    Material.prototype.RenderMusic = function (data) {

    }
    Material.prototype.RenderImg = function (data) {

    }

    Material.prototype.GetVideoTemplate = function (data) {
        var img = data.head_img;
        var id = data.material_id;

        var arr = [];
        arr.push('<div>');
        arr.push('      <img src="' + img + '" alt="" draggable="true" id="material_'+id+'" data-type="video">');
        arr.push('</div>');
        return arr.join('');
    }

    // 拖拽事件
    Material.prototype.events = function(id) {
        var _this = this;

        // console.log('Material.events'+ id);

        var target = $('#material_' + id);
        target.on('dragstart', function(e) {
            // console.log('dragstart');
            e.originalEvent.dataTransfer.setData('id', id);
            // win.dragging = this
        })
        target.on('dragend', function(e) {
            // win.dragging = null
        })
    };
    win.Material = Material;
})(jQuery, window, document);